// pages/home/home.js
import Toast from '@vant/weapp/toast/toast';
import Dialog from '@vant/weapp/dialog/dialog';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showEmpty: false,
    expiredDrugs: [],
    day7List: [],
    keyword: '',
    searchList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    // 请求校验是否为新用户
    const loginRes = await app.call({
      path: '/user/login',
      method: 'POST'
    })
    console.log(loginRes.data)
    if (loginRes.code === 0) {
      if (loginRes.data.isNewUser) {
        wx.navigateTo({
          url: '/pages/home/login/login',
        })
      }
    } else {
      Toast.fail(loginRes.errorMsg);
    }
  },

  async getDateDrugs() {
    // 请求临期药品列表
    const res = await app.call({
      path: '/drug/expireList',
      method: 'GET'
    })
    console.log(res)
    if (res.code === 0) {
      console.log(res.data)
      this.setData({
        day7List: res.data.day7List,
        expiredDrugs: res.data.expiredDrugs
      })
    } else {
      Toast.fail(res.errorMsg);
    }

    this.setData({
      showEmpty: this.data.day7List.length === 0 && this.data.expiredDrugs.length === 0
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getDateDrugs();
    this.getTabBar().init();
  },

  onHide() {

  },

  onChange(event) {
    console.log(event.detail)
    this.setData({
      keyword: event.detail,
    });
  },

  async onSearch(event) {
    console.log(this.data.keyword)
    if (this.data.keyword !== "") {
      wx.navigateTo({
        url: '/pages/resDrugList/resDrugList?from=search&keyword=' + this.data.keyword,
      })
    }
  },

  onOpenSwipeCell(event) {
    this.setData({
      openSwipeCell: true
    })
    console.log(event)
    const id = event.currentTarget.id;
    console.log("id->" + id)
    this.setData({
      swipeCellId: id
    })
  },

  async onCloseExpiredDrugSwipeCell(event) {
    const {
      position,
      instance
    } = event.detail;
    const index = event.currentTarget.dataset.index;
    const expiredDrugs = this.data.expiredDrugs.slice(); // 获取用药计划列表副本
    const delName = expiredDrugs[index].drugName
    switch (position) {
      case 'left':
      case 'cell':
        instance.close();
        break;
      case 'right':
        Dialog.confirm({
          message: '确定删除 ' + delName + ' 吗？',
        }).then(async () => {
          const res = await app.call({
            path: 'drug/deleteDrug?id=' + this.data.swipeCellId,
            method: 'DELETE'
          })
          console.log(res)
          if (res.code === 0) {
            // 获取药品列表
            this.getDateDrugs();
            Toast.success(delName + '\n删除成功');
          } else {
            Toast.fail(res.errorMsg);
          }
        }).catch(() => {
          // on cancel
        });
        instance.close();
        break
    }
    this.setData({
      openSwipeCell: false
    })
  },

  async onCloseDay7DrugSwipeCell(event) {
    const {
      position,
      instance
    } = event.detail;
    const index = event.currentTarget.dataset.index;
    const day7List = this.data.day7List.slice(); // 获取用药计划列表副本
    const delName = day7List[index].drugName
    switch (position) {
      case 'left':
      case 'cell':
        instance.close();
        break;
      case 'right':
        Dialog.confirm({
          message: '确定删除 ' + delName + ' 吗？',
        }).then(async () => {
          const res = await app.call({
            path: 'drug/deleteDrug?id=' + this.data.swipeCellId,
            method: 'DELETE'
          })
          console.log(res)
          if (res.code === 0) {
            // 获取药品列表
            this.getDateDrugs();
            Toast.success(delName + '\n删除成功');
          } else {
            Toast.fail(res.errorMsg);
          }
        }).catch(() => {
          // on cancel
        });
        instance.close();
        break
    }
    this.setData({
      openSwipeCell: false
    })
  },

  onClickSwipeCell(event) {
    const position = event.detail
    if (this.data.openSwipeCell !== true) {
      const id = event.currentTarget.id;
      console.log("id->" + id)
      wx.navigateTo({
        url: '/pages/drugDetail/drugDetail?id=' + id,
      })
    }
  }
})